<template>
  <!--<PageWrapper dense contentFullHeight fixedHeight contentClass="flex">-->
    <!--<BasicTable @register="registerTable" class="w-2/3 xl:w-3/4">-->
      <!--<template #action="{ record }">-->
        <!--<TableAction-->
          <!--:actions="[-->
            <!--{-->
              <!--icon: 'clarity:note-edit-line',-->
              <!--auth: 'menu:edit',-->
              <!--onClick: handleEdit.bind(null, record),-->
            <!--},-->
            <!--{-->
              <!--icon: 'ant-design:delete-outlined',-->
              <!--auth: 'menu:delete',-->
              <!--color: 'error',-->
              <!--popConfirm: {-->
                <!--title: t('是否确认删除'),-->
                <!--confirm: handleDelete.bind(null, record),-->
              <!--},-->
            <!--},-->
          <!--]"-->
        <!--/>-->
      <!--</template>-->
    <!--</BasicTable>-->
    <!--<AccountModal @register="registerModal" @success="handleSuccess" />-->
  <!--</PageWrapper>-->
  <div class="system-user">
    <div>
      <a-form :model="pageForm" class="search-item">
        <a-form-item label="用户名称" class="search-item-box">
          <a-input v-model:value="pageForm.nickName" style="width: 220px;" placeholder="请输入用户名称" />
        </a-form-item>
        <a-form-item label="姓名" class="search-item-box">
          <a-input v-model:value="pageForm.userName" style="width: 220px;" placeholder="请输入姓名" />
        </a-form-item>
        <a-button type="primary" danger class="search-btn" @click="searchbtn">查询</a-button>
      </a-form>
    </div>
    <div style="padding:16px;" class="sticky">
      <a-table :dataSource="dataSource" :columns="columns" :pagination="false" :scroll="{ y: 650 }">
        <template #index="{ index }">
          {{ index + 1 }}
        </template>
        <template #operation="{ record }">
          <a-button class="btn" style="color:#0081ff;" @click="handleRole(record)">设置角色</a-button>
          <a-button class="btn" @click="handleDelete(record)">删除</a-button>
        </template>
      </a-table>
      <div class="otherPagination">
        <div class="seizeASeat"> </div>
        <a-pagination
          size="small"
          :total="pageForm.total"
          :pageSize="pageForm.size"
          :current="pageForm.limit"
          :hideOnSinglePage="true"
          :showSizeChanger="false"
          @change="changePage"
          class="pag"
        />
      </div>
    </div>

    <!-- 设置角色 -->
    <a-modal
      v-model:visible="isAdd"
      :maskClosable="false"
      :width="400"
      :centered="true"
      okText="确认"
      title="修改用户"
      @ok="savaAdd"
      :afterClose="closeModal"
      @cancel="close"
      style="padding: 20px"
    >
      <a-form
        :model="addModelForm"
        :label-col="{ span: 7 }"
        :wrapper-col="12"
        style="margin-top: 16px"
        ref="formRef"
        :rules="rules"
      >
        <a-form-item label="用户名称" name="nickName">
          <a-input
            v-model:value="addModelForm.nickName"
            placeholder="请输入用户名称"
            style="width: 220px"
            type="text"
          />
        </a-form-item>
        <a-form-item label="角色" name="roleName">
          <a-select
            v-model:value="addModelForm.roleName"
            placeholder="请选择角色"
            style="width: 220px"
            allow-clear
          >
            <a-select-option
              v-for="(val, valIndex) in dataSelect"
              :key="valIndex"
              :value="val.id"
            >{{ val.roleName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="手机号码">
          <a-input
            v-model:value="addModelForm.mobile"
            placeholder="请输入手机号码"
            style="width: 220px"
            type="text"
          />
        </a-form-item>
      </a-form>
    </a-modal>
    <!-- 删除用户 -->
    <a-modal v-model:visible="dialogDelete" :maskClosable="false" :width="380" :centered="true" :destroyOnClose="true" title="删除"
             class="remove-model-dialog" @cancel="dialogClose">
      <div class="remove-indicator-tip">是否确定删除</div>
      <template #footer>
        <div class="dialog-footer-box">
          <div class="cancel-btn" @click="dialogClose">取消</div>
          <div class="ok-btn" @click="handleOk">确定</div>
        </div>
      </template>
    </a-modal>


  </div>
</template>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';
  import { getUserPageList,Modify, Delete } from '/@/api/system/user';
  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getRolePageList} from '/@/api/system/role';
  import { useModal } from '/@/components/Modal';
  import { getMenuPermission } from '/@/api/checkToken';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();

  let pageForm = ref({
    size: 10,
    limit: 1,
    total: 0,
    nickName: null, //用户名
    userName: '', //姓名
  });
  getList()
  getRoleList()
  function changePage(val) {
    pageForm.value.limit = val;
    // todo
    getList();
  }
  let formRef = ref(null);
  let dataSource = ref([]);
  let dataSelect = ref([]);
  let columns = ref([
    {
      title: '序号',
      align: 'left',
      width: 100,
      dataIndex: 'index',
      key: 'index',
    },
    {
      title: t('用户名'),
      dataIndex: 'nickName',
      align: 'left',
    },
    {
      title: t('姓名'),
      dataIndex: 'username',
      align: 'left',
    },
    {
      title: t('手机号码'),
      dataIndex: 'mobile',
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      align: 'left',
    },
    {
      title: t('主系统角色'),
      dataIndex: 'mainRoleName',
      align: 'left',
    },
    {
      title: t('本系统角色'),
      dataIndex: 'roleName',
      align: 'left',
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      slots: { customRender: 'operation' },
      align: 'left',
    },
  ]);

  const closeModal = () => {
    formRef.value.resetFields();
  };

  const handleRole = (item) => {
    isAdd.value = true;
    addModelForm.value.nickName=item.nickName;
    addModelForm.value.mobile=item.mobile;
    addModelForm.value.roleName=item.roleId;
    addModelForm.value.roleId=item.roleId;
    addModelForm.value.id=item.id;
  };
  async function getList() {
    let queryData = {
      limit: pageForm.value.limit,
      size: pageForm.value.size,
    };
    if(pageForm.value.userName){
      queryData.userName=pageForm.value.userName
    }
    if(pageForm.value.nickName){
      queryData.nickName=pageForm.value.nickName
    }
    let res = await getUserPageList(queryData);
    let needCurrentPage = res.currentPage;
    dataSource.value = res.list.map((item, index) => {
      return {
        ...item,
        index: needCurrentPage * 10 - 10 + index + 1,
      };
    });

    pageForm.value.total = res.total;
  }
  async function getRoleList() {
    let queryData = {
      limit: pageForm.value.limit,
      size: pageForm.value.size,
    };
    let res = await getRolePageList(queryData);
    dataSelect.value=res.list
  }

  function searchbtn() {
    pageForm.value['pageNum'] = 1;
    pageForm.value['pageSize'] = 20;
    getList();
  }

  //修改用户
  let rules = ref({
    nickName: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
    roleName: [{ required: true, message: '请选择角色', trigger: 'change' }],
    // mobile: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
  });
  const isAdd = ref(false);
  let addModelForm = ref({
    roleId: '', //id
    id: '', //id
    nickName: null, //用户名
    roleName: '', //角色
    mobile: '', //手机号
  });
  async function savaAdd (){
    let queryData = {
      nickName: addModelForm.value.nickName,
      mobile: addModelForm.value.mobile,
      roleId: addModelForm.value.roleName,
    };
    queryData.userIds=[addModelForm.value.id]

    const checked = await formRef.value.validate();
    if (checked) {
      let res = await Modify(queryData);
      isAdd.value = false;
      getList()
      getMenuPermission(localStorage.getItem('userId')).then((res) => {
        localStorage.setItem('menuPermissionList', res.menuPermission);
        window.location.reload()
      }).catch(() => {});
    }
  }
  function close() {

  }
  let userId = ref('');
  const dialogDelete = ref(false);
  function dialogClose() {
    dialogDelete.value=false;
  }
  function handleDelete(record) {
    dialogDelete.value=true;
    userId.value=record.id;
  }
  const { notification } = useMessage();
  async function handleOk (){
    const queryData={
      userIds:[userId.value]
    }
    await Delete(queryData);
    dialogDelete.value=false;
    // notification.success({
    //   message: t('提示'),
    //   description: t('删除成功'),
    // });
    getList()
  };

</script>
<style lang="less">
  .system-user{
    background: #ffffff;
    .search-item{
      display:flex;
      padding: 16px 16px 0 16px;
    }
    .search-item-box{
      display:flex;
      margin-right: 16px;
      /*width: 300px;*/
    }
    .btn{
      border: 0;
      color: #C11A20;
      box-shadow:none;
      background: none;
    }
  }
  .remove-model-dialog {
    .ant-modal-header {
      height: 56px;
      padding: 0px 20px;
      display: flex;
      align-items: center;
    }

    .ant-modal-body {
      padding: 32px 20px !important;
      box-sizing: border-box;

      .remove-indicator-tip {
        color: #303133;
        font-size: 14px;
        font-weight: 500;
        line-height: 22px;
      }
    }

    .ant-modal-footer {
      height: 62px;
      padding: 10px 20px 20px 20px;
      border-top: unset;
      box-sizing: border-box;

      .dialog-footer-box {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        .ok-btn,
        .cancel-btn {
          font-size: 14px;
          padding: 0px 16px;
          border-radius: 4px;
          height: 32px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }

        .cancel-btn {
          border: 1px solid #DCDFE6;
          color: #606266;

          &:hover {
            border-color: #C11A20;
            color: #C11A20;
          }
        }

        .ok-btn {
          background: #C11A20;
          color: #fff;
          margin-left: 12px;
        }
      }
    }

    .ant-modal-close {
      top: 20px;
      right: 20px;

      .ant-modal-close-x {
        width: 16px;
        height: 16px;
        line-height: 1;
      }
    }
  }
</style>
